//
//  GWMainViewController.m
//  网易新闻
//
//  Created by qianfeng on 16/2/28.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWMainViewController.h"
#import "GWLeftMenuView.h"
#import "GWMyNavController.h"
#import "GWNewsViewController.h"
#import "GWTitleView.h"
#import "GWReadingViewController.h"
#import "GWRightViewController.h"
#define GWLeftMenuW 200
#define GWLeftMenuH 300
#define GWLeftMenuY 50
@interface GWMainViewController ()<GWLeftMenuViewDelegate>
@property(nonatomic,weak)GWMyNavController *currentNav;
@property(nonatomic,weak)GWRightViewController *rVC;

@property(nonatomic,weak)GWLeftMenuView *leftView;
@end

@implementation GWMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpViewCOntroller];
   
    [self setUpLeftMenu];
    
    [self setUpRightMenu];
    
}
-(void)setUpRightMenu
{
    GWRightViewController *rVC =[[GWRightViewController alloc]init];
    rVC.view.x =GWScreenW-rVC.view.width;
    [self.view insertSubview:rVC.view atIndex:1];
    [self addChildViewController:rVC];
    self.rVC =rVC;
}
-(void)setUpLeftMenu
{
    GWLeftMenuView *leftView = [[GWLeftMenuView alloc]init];
    leftView.width =GWLeftMenuW;
    leftView.height =GWLeftMenuH;
    leftView.y =GWLeftMenuY;
    leftView.delegate =self;
    [self.view insertSubview:leftView atIndex:1];
    self.leftView =leftView;
}
-(void)setUpViewCOntroller
{
    GWNewsViewController *news =[[GWNewsViewController alloc]init];
    [self setUpViewController:news WithTitle:@"新闻"];
    
    GWReadingViewController *reading =[[GWReadingViewController alloc]init];
    [self setUpViewController:reading WithTitle:@"订阅"];
    
    UIViewController *picture =[[UIViewController alloc]init];
    [self setUpViewController:picture WithTitle:@"图片"];
    UIViewController *video =[[UIViewController alloc]init];
    [self setUpViewController:video WithTitle:@"视频"];
    UIViewController *comment =[[UIViewController alloc]init];
    [self setUpViewController:comment WithTitle:@"跟帖"];
    UIViewController *radio =[[UIViewController alloc]init];
    [self setUpViewController:radio WithTitle:@"电台"];
}
-(void)leftMenuView:(GWLeftMenuView *)leftView didSeletedView:(NSInteger)newView fromView:(NSInteger)oldView
{
    
    GWMyNavController *newNav =self.childViewControllers[newView];
    GWMyNavController *oldNav =self.childViewControllers[oldView];
    
    
    
    newNav.view.layer.shadowColor =[UIColor blackColor].CGColor;
    newNav.view.layer.shadowOffset =CGSizeMake(-5, 0);
    newNav.view.layer.shadowOpacity =0.3;
    [oldNav.view removeFromSuperview];
    
    [self.view addSubview:newNav.view];
    newNav.view.transform =oldNav.view.transform;
    
    self.currentNav =newNav;
    [self maskOnTouch:[self.view viewWithTag:200]];
    
}
-(void)setUpViewController:(UIViewController *)VC WithTitle:(NSString *)title
{
    GWTitleView *titleView =[[GWTitleView alloc]init];
    titleView.title =title;
    VC.navigationItem.titleView =titleView;
    VC.view.backgroundColor =GWRandomColor;
    
    VC.navigationItem.leftBarButtonItem =[UIBarButtonItem barButtonItemWithImageName:@"top_navigation_menuicon" target:self action:@selector(leftBarButtonOnClick)];
    VC.navigationItem.rightBarButtonItem =[UIBarButtonItem barButtonItemWithImageName:@"top_navigation_infoicon" target:self action:@selector(rightBarButtonOnClick)];
    
    GWMyNavController *nav =[[GWMyNavController alloc]initWithRootViewController:VC];
    
    
    [self addChildViewController:nav];
}
-(void)leftBarButtonOnClick
{
    self.leftView.hidden =NO;
    self.rVC.view.hidden =YES;
    [UIView animateWithDuration:0.3 animations:^{
        
        
        CGFloat scale =(GWScreenH-GWLeftMenuY*2)/GWScreenH;
        
        CGFloat x=GWLeftMenuW- GWScreenW *(1-scale)*0.5;
        CGFloat y =GWLeftMenuY- GWScreenH *(1-scale)*0.5;
        CGAffineTransform scaleTrans =CGAffineTransformMakeScale(scale, scale);
        CGAffineTransform translatrForm =CGAffineTransformTranslate(scaleTrans, x/scale , y/scale);
        
        self.currentNav.view.transform =translatrForm;
        
        UIButton *btn =[[UIButton alloc]init];
        btn.tag =200;
        btn.frame =self.currentNav.view.bounds;
        [self.currentNav.view addSubview:btn];
        [btn addTarget:self action:@selector(maskOnTouch:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }];
}
-(void)maskOnTouch:(UIView *)btn
{
    
    [UIView animateWithDuration:0.3 animations:^{
        self.currentNav.view.transform =CGAffineTransformIdentity;
        [btn removeFromSuperview];
    }];
}
-(void)rightBarButtonOnClick
{
    self.leftView.hidden =YES;
    self.rVC.view.hidden =NO;
    [UIView animateWithDuration:0.3 animations:^{
        
        
        CGFloat scale =(GWScreenH-GWLeftMenuY*2)/GWScreenH;
        
        CGFloat x=GWScreenW *(1-scale)*0.5 - self.rVC.view.width ;
        CGFloat y =GWLeftMenuY- GWScreenH *(1-scale)*0.5;
        CGAffineTransform scaleTrans =CGAffineTransformMakeScale(scale, scale);
        CGAffineTransform translatrForm =CGAffineTransformTranslate(scaleTrans, x/scale , y/scale);
        
        self.currentNav.view.transform =translatrForm;
        
        UIButton *btn =[[UIButton alloc]init];
        btn.tag =200;
        btn.frame =self.currentNav.view.bounds;
        [self.currentNav.view addSubview:btn];
        [btn addTarget:self action:@selector(maskOnTouch:) forControlEvents:UIControlEventTouchUpInside];
        
        
    } completion:^(BOOL finished) {
        [self.rVC showAll];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end
